﻿using System;
using System.Xml.Linq;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using DotNetNuke.Entities.Modules;

using DesertedRoadStudios.BlogNetNuke.Components;

namespace DesertedRoadStudios.BlogNetNuke.UI
{
    public class Comment : PortalModuleBase
    {
        private CommentInfo _comment;
        private int _totalComments;
        private int _index;
        private string _relativeURL;
        private XDocument _settings;
        private HtmlControl _commentActionsPane;

        // Comment Details
        public Guid CommentID { get { return _comment.CommentID; } }
        public string CommentTitle { get { return _comment.CommentTitle; } }
        public string CommentAuthor { get { return _comment.AuthorName; } }
        public string CommentURL { get { return ((_comment.URL.StartsWith("http://") || _comment.URL.StartsWith("https://")) ? _comment.URL : "http://" + _comment.URL); } }
        public DateTime CommentDate { get { return _comment.CommentDate; } }
        public string Comments { get { return _comment.Comments; } }
        public bool IsApproved { get { return _comment.IsApproved; } }
        public string CommentLink { get { return "#comment-" + _comment.CommentID.ToString().Replace("-", ""); } }
        public string CommentRelativeURL { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("RelativeURL").Value + "#comment-" + _comment.CommentID.ToString().Replace("-", ""); } }
        public string CommentAbsoluteURL { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("AbsoluteURL").Value + "#comment-" + _comment.CommentID.ToString().Replace("-", ""); } }
        public int Index { get { return _index; } }
        public int TotalComments { get { return _totalComments; } }

        // Post Details
        public Guid PostID { get { return new Guid(_settings.Element("params").Element("Post").Element("PostDetails").Element("PostID").Value); } }
        public string Title { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("PostTitle").Value; } }
        public string Slug { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("Slug").Value; } }
        public string Content { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("Content").Value; } }
        public string Excerpt { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("Excerpt").Value; } }
        public bool AllowComments { get { return Convert.ToBoolean(_settings.Element("params").Element("Post").Element("PostDetails").Element("AllowComments").Value); } }
        public string Status { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("Status").Value; } }
        public DateTime PublishedDate { get { return Convert.ToDateTime(_settings.Element("params").Element("Post").Element("PostDetails").Element("PublishedDate").Value); } }
        public string PostPath { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("PostPath").Value; } }
        public string Keywords { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("Keywords").Value; } }
        public string Description { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("Description").Value; } }
        public string RelativeURL { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("RelativeURL").Value; } }
        public string AbsoluteURL { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("AbsoluteURL").Value; } }
        public string PermaLink { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("PermaLink").Value; } }
        
        // Tab Details
        public int PortalID { get { return Convert.ToInt32(_settings.Element("params").Element("Post").Element("PostDetails").Element("PortalID").Value); } }
        public int TabID { get { return Convert.ToInt32(_settings.Element("params").Element("Post").Element("PostDetails").Element("TabID").Value); } }
        public string TabPath { get { return _settings.Element("params").Element("Post").Element("PostDetails").Element("TabPath").Value; } }
        public int ModuleID { get { return Convert.ToInt32(_settings.Element("params").Element("Post").Element("PostDetails").Element("ModuleID").Value); } }
        
        // User Details
        public DotNetNuke.Entities.Users.UserInfo Author { get { return DotNetNuke.Entities.Users.UserController.GetUserById(Convert.ToInt32(_settings.Element("params").Element("Post").Element("PostDetails").Element("PortalID").Value), Convert.ToInt32(_settings.Element("params").Element("Post").Element("PostDetails").Element("AuthorID").Value)); } }
        
        public Comment()
        {
        }

        public Comment(CommentInfo comment, int totalComments, int index, string relativeURL)
        {
            _comment = comment;
            _totalComments = totalComments;
            _index = index;
            _relativeURL = relativeURL;

            SetSettings();
        }

        private void SetSettings()
        {
            _settings = (XDocument)Session["BlogNetNuke"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserInfo.IsSuperUser)
            {
                _commentActionsPane = FindControl("CommentActionsPane") as HtmlControl;
                if (_commentActionsPane == null)
                    throw new System.Exception("Control must contain an HTML (div) control with ID 'CommentActionsPane'.");

                _commentActionsPane.Controls.Add(new LiteralControl("<div class=\"c_footer\" style=\"text-align:right;\"><span><span>"));

                if (!IsApproved)
                {
                    _commentActionsPane.Controls.Add(new LiteralControl("<span style=\"font-size:10px;\"><em>Submitted by:</em> "));
                    _commentActionsPane.Controls.Add(new LiteralControl("<strong><a href=\"mailto:" + _comment.Email + "\" class=\"CommandButton\" style=\"font-size:10px;font-weight:bold;\">" + _comment.Email + "</a></strong> "));
                    _commentActionsPane.Controls.Add(new LiteralControl("<em>at:</em> <strong>" + _comment.IPAddress + "</strong></span>&nbsp;&nbsp;"));
                    _commentActionsPane.Controls.Add(new LiteralControl("</span>&nbsp;&nbsp;<span>"));

                    ImageButton imApproveComment = new ImageButton();
                    imApproveComment.ToolTip = "Approve";
                    imApproveComment.ImageUrl = "/DesktopModules/BlogNetNuke/images/approve.png";
                    imApproveComment.AlternateText = "Approve";
                    imApproveComment.Attributes.Add("style", "border-width:0px;");
                    imApproveComment.CausesValidation = false;
                    imApproveComment.CommandName = "Approve";
                    imApproveComment.CommandArgument = CommentID.ToString();
                    imApproveComment.Command += new CommandEventHandler(Comment_Command);

                    _commentActionsPane.Controls.Add(imApproveComment);
                    _commentActionsPane.Controls.Add(new LiteralControl("&nbsp;"));

                    LinkButton lbApproveComment = new LinkButton();
                    lbApproveComment.Text = "Approve";
                    lbApproveComment.CssClass = "CommandButton";
                    lbApproveComment.ToolTip = "Approve";
                    lbApproveComment.CausesValidation = false;
                    lbApproveComment.CommandName = "Approve";
                    lbApproveComment.CommandArgument = CommentID.ToString();
                    lbApproveComment.Command += new CommandEventHandler(Comment_Command);

                    _commentActionsPane.Controls.Add(lbApproveComment);
                    _commentActionsPane.Controls.Add(new LiteralControl("</span>&nbsp;&nbsp;<span>"));
                }

                ImageButton imDeleteComment = new ImageButton();
                imDeleteComment.ToolTip = "Delete";
                imDeleteComment.ImageUrl = "/DesktopModules/BlogNetNuke/images/delete.png";
                imDeleteComment.AlternateText = "Delete";
                imDeleteComment.Attributes.Add("style", "border-width:0px;");
                imDeleteComment.CausesValidation = false;
                imDeleteComment.CommandName = "Delete";
                imDeleteComment.CommandArgument = CommentID.ToString();
                imDeleteComment.Command += new CommandEventHandler(Comment_Command);

                _commentActionsPane.Controls.Add(imDeleteComment);
                _commentActionsPane.Controls.Add(new LiteralControl("&nbsp;"));

                LinkButton lbDeleteComment = new LinkButton();
                lbDeleteComment.Text = "Delete";
                lbDeleteComment.CssClass = "CommandButton";
                lbDeleteComment.ToolTip = "Delete";
                lbDeleteComment.CausesValidation = false;
                lbDeleteComment.CommandName = "Delete";
                lbDeleteComment.CommandArgument = CommentID.ToString();
                lbDeleteComment.Command += new CommandEventHandler(Comment_Command);

                _commentActionsPane.Controls.Add(lbDeleteComment);
                _commentActionsPane.Controls.Add(new LiteralControl("</span>&nbsp;&nbsp;</span></div>"));
            }
        }

        void Comment_Command(object sender, CommandEventArgs e)
        {
            BlogNetNukeController bnn = new BlogNetNukeController();

            if (e.CommandName == "Approve")
                bnn.ApproveComment(new Guid(e.CommandArgument.ToString()), _comment.PortalID, _comment.ModuleID, _comment.PostID);
            else if (e.CommandName == "Delete")
                bnn.DeleteComment(new Guid(e.CommandArgument.ToString()), _comment.PortalID, _comment.ModuleID, _comment.PostID);

            Response.Redirect(_relativeURL);
        }

    }
}